package com.ds.infra.rpc.core.util;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Method;

/**
 * 反射调用工具类
 *
 * @author WeiShaoying
 */
@UtilityClass
@Slf4j
public class ReflectionUtils {
    public static Object invokeMethod(Object service, Method method, Object[] args) {
        try {
            return method.invoke(service, args);
        } catch (Exception e) {
            log.error("Reflection invoke failed: {}.{}",
                service.getClass().getSimpleName(),
                method.getName(),
                e);
            throw new RuntimeException("Service invocation error", e);
        }
    }

    public static Method findMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes) {
        try {
            return clazz.getMethod(methodName, paramTypes);
        } catch (NoSuchMethodException e) {
            log.error("Method not found: {}.{}", clazz.getName(), methodName);
            throw new RuntimeException("Method not exist", e);
        }
    }
}
